	var Move = {
		dragTree2Dom : function(treeId, treeNodes) {
			// 				window.console.log("dragTree2Dom");
			// 				window.console.log(arguments);
			if(treeId == "dep_tree"){
				return true;
			}else if(treeId == "emp_tree"){
				return !treeNodes[0].isParent;
			}
		},
		//////////////////////////////不充许在原tree乱放/////////////////////////////////////////
		prevTree : function(treeId, treeNodes, targetNode) {
			window.console.log("prevTree");
			window.console.log(arguments);
			return !targetNode.isParent
					&& targetNode.parentTId == treeNodes[0].parentTId;
		},
		nextTree : function(treeId, treeNodes, targetNode) {
			window.console.log("nextTree");
			window.console.log(arguments);
			return !targetNode.isParent
					&& targetNode.parentTId == treeNodes[0].parentTId;
		},
		innerTree : function(treeId, treeNodes, targetNode) {
			window.console.log("innerTree");
			window.console.log(arguments);
			return targetNode != null && targetNode.isParent
					&& targetNode.tId == treeNodes[0].parentTId;
		},//////////////////////////////End Of 不充许在原tree乱放/////////////////////////////////////////
		dropTree2Dom : function(e, treeId, treeNodes, targetNode, moveType) {
			window.console.log("dropTree2Dom");
			window.console.log(arguments);
			e.preventDefault();
			// 			var domId = "dom_" + treeNodes[0].getParentNode().id;
			// 			if (moveType == null	&& (domId == e.target.id || $(e.target).parents("#" + domId).length > 0)) {
			// 					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			// 					zTree.removeNode(treeNodes[0]);

			// 					var newDom = $("span[domId=" + treeNodes[0].id + "]");
			// 					if (newDom.length > 0) {
			// 						newDom.removeClass("domBtn_Disabled");
			// 						newDom.addClass("domBtn");
			// 					} else {
			var target;
			if (e.target.className == "rule_box") {
				target = e.target;
			} else if (typeof e.target.parentElement != undefined
					&& e.target.parentElement != null
					&& e.target.parentElement.className == "rule_box") {
				target = e.target.parentElement;
			} else {
				target = false;
			}

			if (moveType == null && target) {
				var existDom = $("span[domId=" + treeNodes[0].id + "]");
				if (existDom.length > 0) {
					alert("您已经添加了,请不要重复添加!!!");
				} else {
					if (treeId == "dep_tree"){
						dom_name = treeNodes[0].dep_name;
					}else if(treeId == "emp_tree"){
						dom_name = treeNodes[0].emp_name;
					}
					var new_dom = $("<span class='domBtn'></span>").text(
							dom_name).attr("domId",
							treeNodes[0].id).attr("treeId", treeId);
					$(target).append(new_dom);//.attr({domId:treeNodes[0].id}).text(treeNodes[0].name);
					// 					new_dom.bind("mousedown",rule_obj.remove_dom( treeId, treeNodes[0].id ,this));
					Move.update_rule_obj(treeId, target, treeNodes[0].id);
				}
			} else {
				alert("drop no where!!!");
			}
			// 					}
			// 			Move.updateType();////////////////////////////////////////
			//end of right
			//start error hand
			// 			} 
			// 			else if ($(e.target).parents(".domBtnDiv").length > 0) {
			// 				alert(Move.errorMsg);
			// 			}//end of error hand
		},//end dropTree2Dom
		update_rule_obj : function(treeId, target, id) {
			var obj = new Object();
			obj.treeId = treeId;
			switch (treeId) {
			case "dep_tree":
				obj.id = id;
				if (target.id == "allow_box") {
					obj.is_allow = true;
				} else if (target.id == "disallow_box") {
					obj.is_allow = false;
				}
				rule_obj.department.push(obj);
				break;
			case "emp_tree":
				obj.id = id;
				if (target.id == "allow_box") {
					obj.is_allow = true;
				} else if (target.id == "disallow_box") {
					obj.is_allow = false;
				}
				rule_obj.employee.push(obj);
				break;
			default:
				break;
			}
			//drop 成功后最后一点工作 更新样式 //////////////////
			// 			$("span[treeId = 'dep_tree']").css("background-color","red");
			// 			$("span[treeId = 'emp_tree']").css("background-color","pink");
			window.console.log(rule_obj);

		},
		bindClick : function(e) {
			var target = e.target;
			if (target != null && target.className == "domBtn") {
				rule_obj.remove_dom($(target).attr('treeId'), $(target).attr(
						'domid'), target)
			}
			if (e.preventDefault) {
				e.preventDefault();
			}
		},
		bindDom : function() {
			$("#privilege").bind("click", Move.bindClick);
		},
		// 		updateType : function() {
		// 			var zTree = $.fn.zTree.getZTreeObj("dep_tree"), nodes = zTree
		// 					.getNodes();
		// 			for ( var i = 0, l = nodes.length; i < l; i++) {
		// 				var num = nodes[i].children ? nodes[i].children.length : 0;
		// 				nodes[i].name = nodes[i].name.replace(/ \(.*\)/gi, "") + " ("
		// 						+ num + ")";
		// 				zTree.updateNode(nodes[i]);
		// 			}
		// 		},
		bindSelect : function() {
			return false;
		}
	};

	var rule_obj = {
		privilege_id : null,
		department : [],
		employee : [],
		add_dom : function(type, is_allow, id, name) {
			var obj = new Object();
			var allow_disallow_box_id;
			obj.id = id;
			obj.is_allow = is_allow;
			switch (type) {
			case "department":
				obj.treeId = "dep_tree";
				rule_obj.department.push(obj);
				break;
			case "employee":
				obj.treeId = "emp_tree";
				rule_obj.employee.push(obj);
				break;
			default:
				break;
			}
			if (obj.is_allow == "true" || obj.is_allow == true
					|| obj.is_allow == 1) {
				allow_disallow_box_id = "allow_box";
			} else {
				allow_disallow_box_id = "disallow_box";
			}
			var new_dom = $("<span class='domBtn'></span>").text(name).attr(
					"domId", obj.id).attr("treeId", obj.treeId);
			$("#" + allow_disallow_box_id).append(new_dom);

		},
		remove_dom : function(treeId, id, dom) {
			// 			alert("到了调用了");
			window.console.log("remove_dom", arguments);
			if (treeId == "dep_tree") {
				for ( var i = 0, l = rule_obj.department.length; i < l; i++) {
					if (id == rule_obj.department[i].id) {
						rule_obj.department.splice(i, 1);
						$(".domBtn[domid='" + id + "']").remove();
						// 					 	dom.unbind("click");
					}
				}
			} else if (treeId == "emp_tree") {
				for ( var i = 0, l = rule_obj.employee.length; i < l; i++) {
					if (id == rule_obj.employee[i].id) {
						rule_obj.employee.splice(i, 1);
						$(".domBtn[domid='" + id + "']").remove();
						// 					 	dom.unbind("click");					 	
					}
				}
			}
		},
		post : function(action) { //action 对应 add edit 这里add也类似edit的操作,因js很难一次性把数组提交给服务端
			// 			for ( var i = 0, l = rule_obj.department.length; i < l; i++) {

			// 			}
			// 			for ( var i = 0, l = rule_obj.employee.length; i < l; i++) {

			// 			}
			if (action == "add" ){
				val = $('#privilege_name').val();
				if(val.length > 0){
					rst = $.param({c:"privilegeManager",a:"get_pid",privilege_name:val});
					$.ajax({
						url : "http://10.202.1.109/workForyangtzeu/administrator/index.php",
						data : rst,
						type : "POST",
						success : function(html) {
							rule_obj.privilege_id = html;
							alert("请求ID成功");
						}
	
					});
				}else{
					alert("请输入权限标识名");
					return false;
				}
			}else if(action = "edit") {
				//nothing
			}
			if( privilege_id != null){
				var rst;
				$.each(		rule_obj.department,
								function(i, n) {
									n.c = "departmentManager";
									n.a = "xxx";
									rst = $.param(n);
									$
											.ajax({
												url : "http://10.202.1.109/workForyangtzeu/administrator/index.php",
												data : rst,
												type : "POST",
												success : function(html) {
													alert("ok");
												}
	
											});
								});
	
			}
		}
	}

	var setting = {
		async : {
			enable : true,
			url : "http://10.202.1.109/workForYangtzeu/administrator/index.php?c=departmentManager&a=ajax_tree",
			autoParam : [ "id=dep_id", "department_id", "name=n", "level=lv" ],
			//	 			otherParam : {
			//	 				"otherParam" : "zTreeAsyncTest"
			//	 			},
			dataFilter : filter
		},
		edit : {
			enable : true,
			showRemoveBtn : false,
			showRenameBtn : false,
			drag : {
				prev : Move.prevTree,
				next : Move.nextTree,
				inner : Move.innerTree
			}
		},
		data : {
			keep : {
				parent : true,
				leaf : true
			},
			simpleData : {
				enable : true
			}
		},
		callback : {
			beforeDrag: Move.dragTree2Dom,
			onDrop : Move.dropTree2Dom
		},
		view : {
			selectedMulti : false,
			showLine: false
		}
	};
	/**
	 *
	 */
	function filter(treeId, parentNode, childNodes) {
		// 		window.console.log("filter(treeId, parentNode, childNodes)");
		// 		window.console.log(arguments);
		if (!childNodes)
			return null;

		if ($.isArray(childNodes)) {
			for ( var i = 0, l = childNodes.length; i < l; i++) {
				childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
			}
		} else {
			if (typeof childNodes.error == undefined) {
				return null;
			} else {
				// 				$.dialog(childNodes.error);
				alert(childNodes.error);
				return null;
			}
		}
		return childNodes;
	}

	$(document).ready(function() {
		$.fn.zTree.init($("#dep_tree"), setting);
		setting2 = setting;
		setting2.async.url = "http://10.202.1.109/workForYangtzeu/administrator/index.php?c=employeeManager&a=ajax_tree";
		setting2.async.autoParam = [ "id=dep_id", "employee_id", "name=n", "level=lv" ],
		$.fn.zTree.init($("#emp_tree"), setting2);
		Move.bindDom();
		// 		Move.updateType();/////////////////////////////////
	});
